﻿/**
 * 用于选项卡异步加载数据
 * @param {any} obj
 * @param {any} tab
 * @param {any} className
 * @param {any} savaTabID
 */
function GoToTab(obj, tab, className,savaTabID) {
	if (savaTabID == null || savaTabID == "") {
		savaTabID = "hidTab";
	}
	$("#" + savaTabID).val(tab);
	if (className == null || className == "") {
		className = "current";
	}
	$(obj).addClass(className).siblings().removeClass(className);
	loadTables(0, 15);
}

/**
 * 删除单条数据
 * @param {any} id:要操作数据的ID
 * @param {any} url:为指定的API地址
 * @param {any} callback:回调函数
 */
function DeleteSingle(id, url, callback) {
	layer.confirm("您确认删除选定的记录吗？", {
		btn: ["确定", "取消"]
	}, function () {
		$.ajax({
			type: "POST",
			url: url,
			data: { "id": id },
			headers: {
				"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
			},
			error: function (data, status, e) {
				layer.alert('删除失败!');
			},
			success: function (data) {
				if (data.Result == "ok") {
					layer.msg('删除成功!');
					if (callback != null && typeof callback == "function") {
						callback(0, 15);
					} else {
						setTimeout('window.location.reload()', 1000);
					}
				}
				else {
					layer.alert(data.Result);
				}
			}
		})
	});
};

/**
 * 批量删除
 * @param {any} ids:要操作数据的ID集合
 * @param {any} url:为指定的API地址
 * @param {any} callback:回调函数
 */
function DeleteMulti(ids, url, callback) {
	if (ids == null || ids == "") {
		$(".checkboxs").each(function () {
			if ($(this).prop("checked") == true) {
				ids += $(this).val() + ","
			}
		});
		ids = ids.substring(0, ids.length - 1);
	}
	if (ids.length == 0) {
		layer.alert("请选择要删除的记录。");
		return;
	};
	//询问框
	layer.confirm("您确认删除选定的记录吗？", {
		btn: ["确定", "取消"]
	}, function () {
		var sendData = { "ids": ids };
		$.ajax({
			type: "Post",
			url: url,
			data: sendData,
			headers: {
				"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
			},
			error: function (data, status, e) {
				layer.alert('删除失败!');
			},
			success: function (data) {
				if (data.Result == "ok") {
					layer.msg('删除成功!');
					if (callback != null && typeof callback == "function") {
						callback(0, 15);
					} else {
						setTimeout('window.location.reload()', 1000);
					}
				}
				else {
					layer.alert(data.Result);
				}
			}
		});
	});
};

/**
 * 按照条件删除数据
 * @param {any} url:为指定的API地址
 * @param {any} param:删除参数对像
 * @param {any} callback:回调函数
 */
function DeleteByParam(url, param, callback) {
	if (url == null || url == "") {
		layer.msg("接口地址不存在");
		return false;
	}
	if (param == null || param == "") {
		layer.msg("操作参数不存在");
		return false;
	}
	layer.confirm("您确认删除吗？", {
		btn: ["确定", "取消"]
	}, function () {
		$.ajax({
			type: "POST",
			url: url,
			data: param,
			headers: {
				"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
			},
			error: function (data, status, e) {
				layer.alert('删除失败!');
			},
			success: function (data) {
				if (data.Result == "ok") {
					layer.msg('删除成功!');
					if (callback != null && typeof callback == "function") {
						callback(0, 15);
					} else {
						setTimeout('window.location.reload()', 1000);
					}
				}
				else {
					layer.alert(data.Result);
				}
			}
		})
	});
};

/**
 * 批量更新字段
 * @param {any} url:为指定的API地址
 * @param {any} param:更新参数对像
 * @param {any} callback:回调函数
 */
function UpdateMulti(url, param, callback) {
	if (url == null || url == "") {
		layer.msg("接口地址不存在");
		return false;
	}
	if (param == null || param == "") {
		layer.msg("操作参数不存在");
		return false;
	}
	//询问框
	layer.confirm("您确认要操作选定的记录吗？", {
		btn: ["确定", "取消"]
	}, function () {
		$.ajax({
			type: "Post",
			url: url,
			data: param,
			headers: {
				"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
			},
			error: function (data, status, e) {
				layer.alert('操作失败!');
			},
			success: function (data) {
				if (data.Result == "ok") {
					layer.msg('操作成功!');
					if (callback != null && typeof callback == "function") {
						callback(0, 15);
					} else {
						setTimeout('window.location.reload()', 1000);
					}
				}
				else {
					layer.alert(data.Result);
				}
			}
		});
	});
}

/**
 * 更新开关类型的字段
 * @param {any} objSwitch:开关控件
 * @param {any} url:为指定的API地址
 * @param {any} param:更新参数对像
 */
function UpdateSwitch(objSwitch,url, param) {
	if (url == null || url == "") {
		layer.msg("接口地址不存在");
		return false;
	}
	if (param == null || param == "") {
		layer.msg("操作参数不存在");
		return false;
	}
	//询问框
	layer.confirm("您确认要操作选定的记录吗？"
		, {
			btn: ["确定", "取消"]
		}
		, function () {
			$.ajax({
				type: "Post",
				url: url,
				data: param,
				headers: {
					"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
				},
				error: function (data, status, e) {
					layer.alert('操作失败!');
				},
				success: function (data) {
					if (data.Result == "ok") {
						layer.msg('操作成功!');
						setTimeout('window.location.reload()', 1000);
					}
					else {
						layer.alert(data.Result);
						objSwitch.elem.checked = !objSwitch.elem.checked;
						layui.form.render();
					}
				}
			});
		}
		, function () {
			objSwitch.elem.checked = !objSwitch.elem.checked;
			layui.form.render();
		}
	);
}

/**
 * 用于加载角色列表
 * @param {any} selectID：显示控件的ID；
 * @param {any} selectValue：选中的值；
 * @param {any} isMultiple：控件是否能多选；
 * @param {any} isAddSuperAdmin：是否添加超级管理员；
 */
function loadRolesAjax(selectID, selectValue, isMultiple, isAddSuperAdmin = true) {
	var placeholder = "请选择";
	if (isMultiple) {
		placeholder += "(可以多选)";
	}
	$.ajax({
		type: "GET",
		url: "/Admin/Administrator/GetRoleList?isAddSuperAdmin=" + isAddSuperAdmin+"&_t=" + new Date().getTime(),
		contentType: "application/x-www-form-urlencoded;charset=UTF-8",
		success: function (data) {
			var option = "";
			$.each(data, function (i, item) {
				option += "<option value='" + item.RoleID + "'>" + item.RoleName + "</option>";
			});
			$("#" + selectID).select2({ language: "zh-CN", placeholder: placeholder, allowClear: true, multiple: isMultiple });
			$("#" + selectID).html(option);
			$("#" + selectID).val(selectValue.split(',')).trigger("change");
		}
	});
}

/**
 * 用于加载会员组列表
 * @param {any} selectID：显示控件的ID；
 * @param {any} selectValue：选中的值；
 * @param {any} isMultiple：控件是否能多选；
 */
function loadUserGroupsAjax(selectID, selectValue, isMultiple) {
	var placeholder = "请选择";
	if (isMultiple) {
		placeholder += "(可以多选)";
	}
	$.ajax({
		type: "GET",
		url: "/Admin/User/GetUserGroupsList?_t=" + new Date().getTime(),
		contentType: "application/x-www-form-urlencoded;charset=UTF-8",
		success: function (data) {
			var option = "";
			$.each(data, function (i, item) {
				option += "<option value='" + item.GroupID + "'>" + item.GroupName + "</option>";
			});
			$("#" + selectID).select2({ language: "zh-CN", placeholder: placeholder, allowClear: true, multiple: isMultiple });
			$("#" + selectID).html(option);
			$("#" + selectID).val(selectValue.split(',')).trigger("change");
		}
	});
}

/**
 * 初始化所属业务员控件
 * @param {any} RoleID 业务经理所属的角色ID，为0时，取上级管理员为空的数据
 * @param {any} SalesManager 业务经理
 * @param {any} Sales 业务员
 * @param {any} PrefixID 控件ID前缀
 */
function InitMySales(RoleID, SalesManager, Sales, PrefixID) {
	if (!RoleID || RoleID == "") {
		RoleID = 0;
	}
	var param1 = { "Action": "SalesManager", "RoleID": RoleID, "SalesManager": SalesManager };
	DataBindMySales(param1, PrefixID + "SalesManager");
	var param2 = { "Action": "Sales", "SalesManager": SalesManager, "Sales": Sales };
	DataBindMySales(param2, PrefixID + "Sales");
}
function DataBindMySales(param, ctlID) {
	var obj = GetID(ctlID);
	if ($(obj).length == 0) {
		return;
	}
	var url = '/Common/MySales';
	$.ajax({
		type: "POST",
		url: url,
		data: param,
		async: false,
		headers: {
			"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
		},
		error: function (data, status, e) {
			layer.alert('加载失败!');
		},
		success: function (returnData) {
			if (returnData.Status == "1") {
				$(obj).empty();
				$(obj).append(returnData.Data);
			}
		}
	});
}
function SelectSalesManager(PrefixID) {
	var param = { "Action": "Sales", "SalesManager": GetJQ(PrefixID + "SalesManager").val(), "Sales": "" };
	DataBindMySales(param, PrefixID + "Sales");
}

/**
 * 从商户中移除
 * @param {any} UserName：会员名
 */
function RemoveFromCompany(UserName) {
	layer.confirm("您确认要从商户中移除吗？", {
		btn: ["确定", "取消"]
	}, function () {
		$.ajax({
			type: "POST",
			url: "/Admin/User/RemoveFromCompany",
			data: { "UserName": UserName },
			headers: {
				"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
			},
			error: function (data, status, e) {
				layer.alert('移除失败!');
			},
			success: function (data) {
				if (data.Result == "ok") {
					layer.msg('移除成功!');
					setTimeout('window.location.reload()', 1000);
				}
				else {
					layer.alert(data.Result);
				}
			}
		})
	});
}
/**
 * 升级为商户管理员
 * @param {any} UserName：会员名
 */
function AddToCompanyAdmin(UserName) {
	layer.confirm("您确认要升级吗？", {
		btn: ["确定", "取消"]
	}, function () {
		$.ajax({
			type: "POST",
			url: "/Admin/User/AddToCompanyAdmin",
			data: { "UserName": UserName },
			headers: {
				"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
			},
			error: function (data, status, e) {
				layer.alert('升级失败!');
			},
			success: function (data) {
				if (data.Result == "ok") {
					layer.msg('升级成功!');
					setTimeout('window.location.reload()', 1000);
				}
				else {
					layer.alert(data.Result);
				}
			}
		})
	});
}
/**
 * 降为普通成员
 * @param {any} UserName：会员名
 */
function RemoveFromCompanyAdmin(UserName) {
	layer.confirm("您确认要降级吗？", {
		btn: ["确定", "取消"]
	}, function () {
		$.ajax({
			type: "POST",
			url: "/Admin/User/RemoveFromCompanyAdmin",
			data: { "UserName": UserName },
			headers: {
				"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
			},
			error: function (data, status, e) {
				layer.alert('降级失败!');
			},
			success: function (data) {
				if (data.Result == "ok") {
					layer.msg('降级成功!');
					setTimeout('window.location.reload()', 1000);
				}
				else {
					layer.alert(data.Result);
				}
			}
		})
	});
}
/**
 * 批准加入
 * @param {any} UserName
 * @param {any} companyClientId
 */
function AgreeJoinCompany(UserName, companyClientId) {
	layer.confirm("您确认要批准加入吗？", {
		btn: ["确定", "取消"]
	}, function () {
		$.ajax({
			type: "POST",
			url: "/Admin/User/AgreeJoinCompany",
			data: { "UserName": UserName, "companyClientId": companyClientId },
			headers: {
				"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
			},
			error: function (data, status, e) {
				layer.alert('批准加入失败!');
			},
			success: function (data) {
				if (data.Result == "ok") {
					layer.msg('批准加入成功!');
					setTimeout('window.location.reload()', 1000);
				}
				else {
					layer.alert(data.Result);
				}
			}
		})
	});
}
/**
 * 拒绝加入
 * @param {any} UserName：会员名
 */
function RejectToCompany(UserName) {
	layer.confirm("您确认要拒绝加入吗？", {
		btn: ["确定", "取消"]
	}, function () {
		$.ajax({
			type: "POST",
			url: "/Admin/User/RemoveFromCompany",
			data: { "UserName": UserName },
			headers: {
				"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
			},
			error: function (data, status, e) {
				layer.alert('拒绝加入失败!');
			},
			success: function (data) {
				if (data.Result == "ok") {
					layer.msg('拒绝加入成功!');
					setTimeout('window.location.reload()', 1000);
				}
				else {
					layer.alert(data.Result);
				}
			}
		})
	});
}